import Vue from "vue"
import Vuex from "vuex"
import cart from "./cart"
import product from "./product"
import createPersistedState from "vuex-persistedstate"

Vue.use(Vuex)

let store = new Vuex.Store({
    // strict:true,
    state:{
        name: '小明',
        age: '18',
        nickname: '小不点',
        box:[],
    },
    getters:{
        reverseN(state) {
            return state.nickname.split('').reverse().join("")
        }
    },
    mutations:{
        INCREASE: function (state,payload){
            state.age++
            console.log(payload)
        },
        DECREASE: function (state,payload){
            state.age--
            console.log(payload)
        },
        SET_PRO(state,payload){
            state.box = payload
        }
    },
    actions:{
        FETCH_PRO(context, payload) {
            console.log(context)
            fetch('http://chst.vip/data/index.json')
            .then(body=>body.json())
            .then(res=>{
                // console.log(res)
                context.commit('SET_PRO',res.data)
            })
        }
    },
    modules:{
        cart,
        product
    },
    plugins:[
        createPersistedState({
            storage:window.localStorage,
            key:'nick',
            paths:['nickname','product.products']
        })
    ]
})

export default store